package cn.hs.ph.dao.impl;


import cn.hs.ph.dao.CommunityDao;
import cn.hs.ph.entity.Community;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CommunityDaoImpl implements CommunityDao {
    @Override
    public int insert(Community community) {
        String sql = " INSERT INTO community ";
        sql += " values(null,?," +
                "?,?,?,?," +
                "?,?,?," +
                "?,null,null,null," +
                "?,null) ";
        Object[] obj = {
//        name
                community.getCommunityName(),
//        省市区地点
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
//        经纬 时间
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                new Date(),
//        关联门店
                community.getCommunityShopCount(),
//        用户数量
                community.getCommunityUserCount(),
        };
//        最终添加
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public int delete(int communityId) {
        String sql = " delete from community where communityId=? ";
        Object[] obj = {communityId};
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public int update(Community community) {
        String sql = "UPDATE community SET " +
//          名称
                " communityName=?, " +
//          省市区地点
                " communityProvince=?, " +
                " communityCity=?, " +
                " communityDistrict=?, " +
                " communityAddress=?, " +
//          经纬
                " communityLatitude=?, " +
                " communityLongitude=? " +
                " WHERE communityId=? ";
        Object[] obj = {
//                名称
                community.getCommunityName(),
//                省市区地点
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
//                经纬
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
//                修改ID
                community.getCommunityId()
        };
//        最终修改
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public List<Community> selectAll(Map<String, String> map) {
        List<Community> list = new ArrayList<>();
//        动态查询
        String sql = "select * from community where 1=1 ";
        List<Object> params = new ArrayList<>();
//        模糊查询
        String communityName = map.get("communityName");
        if (communityName != null && !"".equals(communityName)) {
            sql += " and communityName like ? ";
            params.add("%" + communityName + "%");
        }
//        时间范围
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and createTime between ? and ? ";
            params.add(startTime);
            params.add(endTime);
        }

//        倒序
        sql += " order by communityId desc ";
//        分页查询
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?, ? ";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }
//        最终查询
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                Community community = new Community(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getDate(9),
                        res.getInt(10),
                        res.getInt(11),
                        res.getString(12),
                        res.getInt(13),
                        res.getInt(14),
                        res.getString(15)
                );
                list.add(community);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count = 0;
        List<Community> list = new ArrayList<>();
//        动态查询
        String sql = " select count(1) from community where 1=1 ";
        List<Object> params = new ArrayList<>();
//        模糊查询
        String communityName = map.get("communityName");
        if (communityName != null && !"".equals(communityName)) {
            sql += " and communityName like ? ";
            params.add("%" + communityName + "%");
        }
//        时间范围
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and createTime between ? and ? ";
            params.add(startTime);
            params.add(endTime);
        }
//        最终查询
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                count = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return count;
    }
    @Override
    public int updateShopCount(int count, int communityId) {
        int num=0;
        String sql="update community set communityShopCount=communityShopCount+? where communityId=?";
        Object[] obj={
                count,
                communityId
        };
        num = JDBCDruidUtil.update(sql, obj);
        return num;
    }
}
